package com.phone.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seventh.common.pojo.R;
import com.phone.client.mapper.LevelMapper;
import com.phone.client.poji.entry.TbMemberLevel;
import com.phone.client.poji.vo.LevelVo;
import com.phone.client.service.LevelService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: clms-seventh
 * @description: LevelService的实现
 * @author: 杨佳琪
 * @create: 2021-07-24 09:23
 **/
@ApiModel(value="会员等级service的实现", description="会员等级service的实现")
@Service
public class LevelServiceImp implements LevelService {
    @Resource
    private LevelMapper levelMapper;

    @ApiModelProperty("分页查询")
    @Override
    public R findAllLeveltByPage(Integer currentPage, Integer pageSize, LevelVo levelVo) {
        Page<TbMemberLevel> page=new Page<>(currentPage,pageSize);
        QueryWrapper<TbMemberLevel> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotEmpty(levelVo.getLevname())){
            wrapper.like("levname",levelVo.getLevname());
        }

        Page<TbMemberLevel> page1=levelMapper.selectPage(page,wrapper);
        return R.ok().put("records",page1.getRecords()).put("total",page1.getTotal());
    }

    @ApiModelProperty("删除")
    @Override
    public R deleteLevel(Integer id) {  
        int i=levelMapper.deleteById(id);
        return R.ok();
    }

    @ApiModelProperty("修改")
    @Override
    public R updateLevel(TbMemberLevel tbMemberLevel) {
        levelMapper.updateById(tbMemberLevel);
        return R.ok();
    }

    @ApiModelProperty("新增")
    @Override
    public R insertLevel(TbMemberLevel tbMemberLevel) {
        levelMapper.insert(tbMemberLevel);
        return R.ok();
    }

    @ApiModelProperty("修改的回显")
    @Override
    public R findElvelById(Integer id) {
        TbMemberLevel level=levelMapper.selectById(id);
        return R.ok().put("level",level);
    }

    @ApiModelProperty("查询等级名称是否唯一")
    @Override
    public R selName(TbMemberLevel tbMemberLevel) {
        List<TbMemberLevel> list=levelMapper.selectList(null);
        int result=0;
        for (TbMemberLevel u:list) {
            if (tbMemberLevel.getId()!=null){
                if (!u.getId().equals(tbMemberLevel.getId())){
                    if (u.getLevname().equals(tbMemberLevel.getLevname())){
                        result=1;
                        break;
                    }
                }
            }else {
                if (u.getLevname().equals(tbMemberLevel.getLevname())){
                    result=1;
                    break;
                }
            }
        }
        return R.ok().put("code",result);
    }

    @Override
    public R findMumber() {
        return R.ok().put("level",levelMapper.selectList(null));
    }


}



